﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using DBLOG.BizTalk.BizTalkExplorerServices.Entities;
using DBLOG.BizTalk.BizTalkExplorerServices.Core;
using DBLOG.BizTalk.BizTalkExplorerServices.Explorers;

namespace DBLOG.BizTalk.BizTalkExplorerServices.Services
{
    public class BizTalkExplorer : IBizTalkExplorer
    {
        #region IBizTalkExplorer Members

        /// <summary>
        /// Returns a list of BizTalk errors.
        /// The following items are considered errors: suspended messages and suspended orchestration instances.
        /// </summary>
        /// <returns></returns>
        List<BTSError> IBizTalkExplorer.GetErrors()
        {
            using (ErrorExplorer errorExplorer = new ErrorExplorer())
            {
                return errorExplorer.GetErrors();
            }
        }

        /// <summary>
        /// Returns a list of BizTalk warnings.
        /// The following items are considered warnings: send ports, receive locations and orchestrations that have a status of 'stopped'.
        /// </summary>
        /// <returns></returns>
        List<BTSWarning> IBizTalkExplorer.GetWarnings()
        {
            using (WarningExplorer warningExplorer = new WarningExplorer())
            {
                return warningExplorer.GetWarnings();
            }
        }

        BTSSummary IBizTalkExplorer.GetSummary()
        {
            using (SummaryExplorer summaryExplorer = new SummaryExplorer())
            {
                return summaryExplorer.GetSummary();
            }
        }

        List<BTSReceiveLocation> IBizTalkExplorer.GetReceiveLocations()
        {
            using (ReceiveLocationExplorer receiveLocationExplorer = new ReceiveLocationExplorer())
            {
                var btsReceiveLocations = from r in receiveLocationExplorer.GetReceiveLocations()
                                          select new BTSReceiveLocation(r);

                return btsReceiveLocations.ToList<BTSReceiveLocation>();
            }
        }

        List<BTSReceiveLocation> IBizTalkExplorer.GetReceiveLocations(string receivePortName)
        {
            using (ReceiveLocationExplorer receiveLocationExplorer = new ReceiveLocationExplorer())
            {
                var btsReceiveLocations = from r in receiveLocationExplorer.GetReceiveLocations()
                                          where r.ReceivePort.Name == receivePortName
                                          select new BTSReceiveLocation(r);

                return btsReceiveLocations.ToList<BTSReceiveLocation>();
            }
        }

        List<BTSMessage> IBizTalkExplorer.GetMessages()
        {
            using (MessageExplorer messageExplorer = new MessageExplorer())
            {
                var messages = from m in messageExplorer.GetMessages()
                               select new BTSMessage(m);

                return messages.ToList<BTSMessage>();
            }
        }

        List<BTSMessage> IBizTalkExplorer.GetSuspendedMessages()
        {
            using (MessageExplorer messageExplorer = new MessageExplorer())
            {
                var suspendedMessages = from m in messageExplorer.GetSuspendedMessages()
                                        select new BTSMessage(m);

                return suspendedMessages.ToList<BTSMessage>();
            }
        }

        List<BTSApplication> IBizTalkExplorer.GetApplications()
        {
            using (ApplicationExplorer applicationExplorer = new ApplicationExplorer())
            {
                var btsApplications = from a in applicationExplorer.GetApplications()
                                      select new BTSApplication(a);

                return btsApplications.ToList<BTSApplication>();
            }
        }

        List<BTSSendPort> IBizTalkExplorer.GetSendPorts()
        {
            using (SendPortExplorer sendPortExplorer = new SendPortExplorer())
            {
                var btsSendPorts = from s in sendPortExplorer.GetSendPorts()
                                   select new BTSSendPort(s);

                return btsSendPorts.ToList<BTSSendPort>();
            }
        }

        List<BTSSendPort> IBizTalkExplorer.GetSendPorts(string applicationName)
        {
            using (SendPortExplorer sendPortExplorer = new SendPortExplorer())
            {
                var btsSendPorts = from s in sendPortExplorer.GetSendPorts()
                                   where s.Application.Name == applicationName
                                   select new BTSSendPort(s);

                return btsSendPorts.ToList<BTSSendPort>();
            }
        }

        List<BTSReceivePort> IBizTalkExplorer.GetReceivePorts()
        {
            using (ReceivePortExplorer receivePortExplorer = new ReceivePortExplorer())
            {
                var btsReceivePorts = from r in receivePortExplorer.GetReceivePorts()
                                      select new BTSReceivePort(r);

                return btsReceivePorts.ToList<BTSReceivePort>();
            }
        }

        List<BTSReceivePort> IBizTalkExplorer.GetReceivePorts(string applicationName)
        {
            using (ReceivePortExplorer receivePortExplorer = new ReceivePortExplorer())
            {
                var btsReceivePorts = from r in receivePortExplorer.GetReceivePorts()
                                      where r.Application.Name == applicationName
                                      select new BTSReceivePort(r);

                return btsReceivePorts.ToList<BTSReceivePort>();
            }
        }

        List<BTSOrchestration> IBizTalkExplorer.GetOrchestrations()
        {
            using (OrchestrationExplorer orchestrationExplorer = new OrchestrationExplorer())
            {
                var btsOrchestrations = from o in orchestrationExplorer.GetOrchestrations()
                                        select new BTSOrchestration(o);

                return btsOrchestrations.ToList<BTSOrchestration>();
            }
        }

        List<BTSOrchestration> IBizTalkExplorer.GetOrchestrations(string applicationName)
        {
            using (OrchestrationExplorer orchestrationExplorer = new OrchestrationExplorer())
            {
                var btsOrchestrations = from o in orchestrationExplorer.GetOrchestrations()
                                        where o.Application.Name == applicationName
                                        select new BTSOrchestration(o);

                return btsOrchestrations.ToList<BTSOrchestration>();
            }
        }

        #endregion
    }
}
